﻿using BiztalkConfigLoader.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BiztalkConfigLoader
{
    public partial class Main : Form
    {

        public Model.BtsAdminConfiguration CurrentConfiguration { get; set; }
        public Main()
        {
            InitializeComponent();
            treeView1.AfterSelect += treeView1_AfterSelect;
            InitConfig();
        }

        void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.GetType() == typeof(TreeNode))
            {
                SetupButtons();
                btnDelete.Enabled = false;
                btnEdit.Enabled = false;
                btnAdd.Enabled = true;
            }

            if (e.Node.GetType() == typeof(InstanceNode))
            {
                SetupButtons();
                btnDelete.Enabled = true;
                btnEdit.Enabled = true;
                btnAdd.Enabled = false;
            }

            if (e.Node.GetType() == typeof(AdapaterNode))
            {
                SetupButtons();
                btnDelete.Enabled = true;
                btnEdit.Enabled = true;
                btnAdd.Enabled = false;
            }
            if (e.Node.GetType() == typeof(HostNode))
            {
                SetupButtons();
                btnDelete.Enabled = true;
                btnEdit.Enabled = true;
                btnAdd.Enabled = false;
            }



        }

        private void SetupButtons()
        {
            var txt = treeView1.SelectedNode.Text;
            btnDelete.Text = "Del " + txt;
            btnAdd.Text = "Add " + txt;
            btnEdit.Text = "Edit " + txt;
        }

        private void btnCreateConfig_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Selecteer BTS Config File to import";
            openFileDialog1.DefaultExt = "xml";
            Model.BtsAdminConfiguration config;
            var result = openFileDialog1.ShowDialog(this);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                config = Model.BtsAdminConfiguration.LoadFromFile(openFileDialog1.FileName);
                CurrentConfiguration = config;

            }
            else
            {
                InitConfig();
            }

            fillTreeView(CurrentConfiguration);
        }

        private void InitConfig()
        {
            CurrentConfiguration = new BtsAdminConfiguration();
            CurrentConfiguration.Adapters = new List<BtsAdminConfigurationAdapter>();
            CurrentConfiguration.HostInstances = new List<BtsAdminConfigurationHostInstance>();
            CurrentConfiguration.Hosts = new List<BtsAdminConfigurationHost>();
        }

        private void fillTreeView(BtsAdminConfiguration config)
        {

            

            treeView1.Nodes.Clear();
            var hostnode = new TreeNode("Hosts");
            var instancenode = new TreeNode("Instances");
            var adapternode = new TreeNode("Adapters");

            treeView1.Nodes.Add(hostnode);
            treeView1.Nodes.Add(instancenode);
            treeView1.Nodes.Add(adapternode);
            if (config != null)
            {

                

                foreach (var hostinstance in config.HostInstances)
                {
                    InstanceNode item = new InstanceNode(hostinstance);

                    instancenode.Nodes.Add(item);
                    instancenode.ExpandAll();

                }

                foreach (var host in config.Hosts)
                {
                    HostNode item = new HostNode(host);

                    hostnode.Nodes.Add(item);
                    hostnode.ExpandAll();

                }

                foreach (var adapter in config.Adapters)
                {
                    AdapaterNode item = new AdapaterNode(adapter);

                    adapternode.Nodes.Add(item);
                    adapternode.ExpandAll();

                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            object newobject = null;
            object itemtoadd = null;
            if (treeView1.SelectedNode != null)
            {




                switch (treeView1.SelectedNode.Text)
                {
                    case "Hosts":
                        newobject = new Model.BtsAdminConfigurationHost();
                        itemtoadd = GetNewNode(newobject) as Model.BtsAdminConfigurationHost;
                        if (itemtoadd != null)
                        {
                            CurrentConfiguration.Hosts.Add(itemtoadd as Model.BtsAdminConfigurationHost);
                            fillTreeView(CurrentConfiguration);
                        }
                        break;
                    case "HostInstances":
                        newobject = new BtsAdminConfigurationHostInstance();
                        itemtoadd = GetNewNode(newobject) as BtsAdminConfigurationHostInstance;
                        if (itemtoadd != null)
                        {
                            CurrentConfiguration.HostInstances.Add(itemtoadd as BtsAdminConfigurationHostInstance);
                            fillTreeView(CurrentConfiguration);
                        }
                       
                        break;
                    case "Adapters":
                        newobject = new Model.BtsAdminConfigurationAdapter();
                        itemtoadd = GetNewNode(newobject) as Model.BtsAdminConfigurationAdapter;
                        if (itemtoadd != null)
                        {
                            CurrentConfiguration.Adapters.Add(itemtoadd as Model.BtsAdminConfigurationAdapter);
                            fillTreeView(CurrentConfiguration);
                        }                    
                        break;
                    default:
                        break;
                }
                btnAdd.Enabled = false;
            }
        }
        private static object GetNewNode(Object newobject)
        {
            AddObject frmedit = new AddObject() { ObjectToAdd = newobject };
            object returnedobject = null;
            var result = frmedit.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                var objecttoadd = frmedit.ObjectToAdd;
                returnedobject = objecttoadd;
            }
            return returnedobject;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var selectedobject = treeView1.SelectedNode.Tag;
            if (selectedobject.GetType() == typeof(BtsAdminConfigurationHost))
            {

                CurrentConfiguration.Hosts.Remove(selectedobject as BtsAdminConfigurationHost);
                
            }
            fillTreeView(CurrentConfiguration);
        }
    }
}
